package com.spring.common.entity;

import com.spring.common.annotation.SexValid;
import com.spring.common.annotation.StuCodeValid;
import com.spring.common.validate.AddStuAndTeach;
import com.spring.common.validate.AddStudent;
import com.spring.common.validate.QueryDetail;
import lombok.Data;
import org.hibernate.validator.constraints.Length;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 总结：在接口的入口方法参数上、校验参数上都注明了分组的信息，那么接口被用的时候，
 * 就可以根据不同的分组信息执行不同约束注解的校验逻辑了，这个能力是spring-validation提供的，
 * 所以这种场景下，controller层方法的上注解要用@Validated，@Valid注解没有这种能力。
 * @author Gary
 */
@Data
public class Student {

    @NotNull(message = "学生id不能为空", groups = QueryDetail.class)
    private Integer id;
    @NotNull(message = "学号不能为空", groups = AddStudent.class)
    @Length(min = 2, max = 4, message = "学号的长度范围是(2,4)")
    @StuCodeValid(groups = AddStudent.class,message = "学生的学号不能重复")
    private String stuCode;
    @NotNull(message = "姓名不能为空", groups = AddStudent.class)
    @Length(min = 2, max = 3, message = "姓名的长度范围是(2,3)", groups = AddStudent.class)
    private String stuName;
    @SexValid(groups = AddStudent.class)
    private String sex;

    @Valid
    @NotNull(message = "学生的老师不能为空",groups = AddStuAndTeach.class)
    private Teacher teacher;
}
